/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
  */
@isTest
private class TestLeadTrigger 
{
    static testMethod void TestInsert() 
    {
        List<Lead> Leads = new List<Lead>();    
   
        Lead a;
        
        // Test 0       
        a = new Lead();
        a.FirstName = 'UnitTest';
        a.LastName = 'UnitTest';
        a.Company = 'UnitTest';
        a.Phone = '(415) 000-0000';
        a.Zip_Postal_Code__c = '94555';
        a.LeadSource = 'AOL';
        a.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        Leads.add(a);

    
        Test.startTest();
        insert Leads;
        Test.stopTest();
        
        Map<ID, Lead> savedLeads = new Map<ID, Lead>([SELECT CleanPhoneText__c from Lead where ID in :Leads]);
                    
        System.assertNotEquals(null, savedLeads.get(Leads[0].ID).CleanPhoneText__c);
        
    }


    static testMethod void TesUpdate() 
    {
        List<Lead> Leads = new List<Lead>();    
   
        Lead a;
        
        // Test 0       
        a = new Lead();
        a.FirstName = 'UnitTest1';
        a.LastName = 'UnitTest1';
        a.Company = 'UnitTest1';
        a.Phone = '(415) 111-1111';
        a.Zip_Postal_Code__c = '94555';
        a.LeadSource = 'AOL';
        a.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        insert a;
        a.Phone = '(415) 222-2221';
        Leads.add(a);
        
        // Test 1       
        a = new Lead();
        a.FirstName = 'UnitTest2';
        a.LastName = 'UnitTest2';
        a.Company = 'UnitTest2';
        a.Phone = '(415) 333-3333';
        a.Zip_Postal_Code__c = '94555';
        a.LeadSource = 'AOL';
        a.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        insert a;
        Leads.add(a);
        
        
        
        Test.startTest();
        update Leads;
        Test.stopTest();
        
        Map<ID, Lead> savedLeads = new Map<ID, Lead>([SELECT CleanPhoneText__c from Lead where ID in :Leads]);
                    
        System.assertNotEquals(null, savedLeads.get(Leads[0].ID).CleanPhoneText__c);
        System.assertNotEquals(null, savedLeads.get(Leads[1].ID).CleanPhoneText__c);
    }

    private static string localAdvertiserLeadRecordType;
    private  static ID GetLocalAdvertiserLeadRecordType()
    {       
        if(localAdvertiserLeadRecordType == null)
            localAdvertiserLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
        
        return localAdvertiserLeadRecordType;
    }
}